<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Session;
class Integral  extends Common
{
	public function index()
	{
		return $this->view->fetch();
	}
	public function allshops()
	{
			//数量
	        $leaveCount = Db::name("integral_shop")->where($where)->count();

	        //分页数据
	        $limit=input('limit');
	        $page=input('page');
	        $start=($page-1)*$limit;

	        //调取数据
	        $userall=Db::name("integral_shop")->where($where)->limit($start,$limit)->order('id desc')->select();

	        //API数据格式处理
	        $data= array();
	        $data['code']=0;
	        $data['msg']='';
	        $data['count']=$leaveCount;
	        $data['data']=$userall;

	        //返回json数据
	        echo $data=json_encode($data);
	}
	public function upload(){
        	if(Request::instance()->isPost()){
	            $file = request()->file();
	            $fileinfo=$file['file']->getInfo(); 
	            
	            $fileurl=$fileinfo['tmp_name'];
	            $filetype=explode('/',$fileinfo['type']);
	            $filetype=$filetype[1];
	            $filename=time().rand(0,9).'.'.$filetype;
	            $copyurl='public/uploads/'.$filename;


	            if(!copy($fileurl,$copyurl)){
	                $fileinfo['error']=000000;
	            };
	          
	            $imgrl = array();
	            $imgrl['code']=$fileinfo['error'];
	            $imgrl['msg']=00;
	            $imgrl['data']['src']='/'.$copyurl;
	            $imgrl['data']['tite']=$fileinfo['name'];

	            $img=json_encode($imgrl);
	            return $img;
	        }
        }
	public function addshop()
	{
		return $this->view->fetch();
	}
	public function editimg()
	{
		if(Request::instance()->isPost()){
	            $file = request()->file();
	            $fileinfo=$file['file']->getInfo(); 
	            
	            $fileurl=$fileinfo['tmp_name'];
	            $filetype=explode('/',$fileinfo['type']);
	            $filetype=$filetype[1];
	            $filename=time().rand(0,9).'.'.$filetype;
	            $copyurl='public/uploads/'.$filename;


	            if(!copy($fileurl,$copyurl)){
	                $fileinfo['error']=000000;
	            }
	            ;
	          
	            $imgrl = array();
	            $imgrl['code']=$fileinfo['error'];
	            $imgrl['msg']=00;
	            $imgrl['data']['src']='/'.$copyurl;
	            $imgrl['data']['tite']=$fileinfo['name'];

	            $img=json_encode($imgrl);
	            return $img;
	        }
	}
	public function saveshop(){
			$content=input('content');
	        $datas=input('data');
	        $datas=json_decode($datas,true);
	        $save['title']=$datas['title'];
	        $save['num']=$datas['num'];
	        $save['pay']=$datas['pay'];
	        $save['img']=$datas['img'];
	        $save['content']=$content;
	        $save['addtime']=date('Y-m-d H:i:s',time());


	        $user=SESSION::get('cmuser');
	        $log_title=$datas['title'];
	        $log="add integral shop ' $log_title '";
	        set_log($user,$log);

	        if($datas){
	            $chit=Db::name('integral_shop');
	            $is_insert=$chit->insert($save);
	            if($is_insert){
	                return '1';
	            }else{
	                return '99';
	            }
	        }else{
	            return '参数错误';
	        }
	}
	public function delshop()
	{
	        $id=input('data');
	        $chit=Db::name('integral_shop');
	        $check=$chit->delete($id);

	        $user=SESSION::get('cmuser');
	        $log_title=$id;
	        $log="delete integral shop ' $log_title '";
	        set_log($user,$log);

	        if($check){
	            return '1';
	        }else{
	            return '99';
	        }
	}
	public function delshops()
	{
			$datas=input('data');
		    $datas=json_decode($datas,true);

		    $chit=Db::name('integral_shop');
		    foreach ($datas as $k => $v) {
		        $del['id']= $v['id'];
		        $chit->where($del)->delete();
		        $log_id=$log_id.'|'.$v['id'];
		    }

		    $user=SESSION::get('cmuser');
	        $log_title=$log_id;
	        $log="delete integral shop ' $log_title '";
	        set_log($user,$log);


		    return '1';
	}
	public function editshop()
	{
		$id=input('id');
        $info=Db::name('integral_shop')->where(array('id' =>$id))->find();
        $this->view->assign('info', $info);
		return $this->view->fetch();
	}
	public function editsaveshop()
	{
			$content=input('content');
	        $datas=input('data');
	        $datas=json_decode($datas,true);
	        $where['id']=$datas['id'];
	        $save['title']=$datas['title'];
	        $save['num']=$datas['num'];
	        $save['img']=$datas['img'];
	        $save['pay']=$datas['pay'];
	        $save['content']=$content;
	        $save['updatetime']=date('Y-m-d H:i:s',time());

	        $user=SESSION::get('cmuser');
	        $log_title=$datas['title'];
	        $log="edit integral shop ' $log_title ' save";
	        set_log($user,$log);


	        if($datas){
	            $chit=Db::name('integral_shop');
	            $is_update=$chit->where($where)->update($save);
	            if($is_update){
	                return '1';
	            }else{
	                return '99';
	            }
	        }else{
	            return '参数错误';
	        }
	}
}

